package com.qitmiaojie.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qitmiaojie.controller.utils.Result;
import com.qitmiaojie.pojo.Malfunction;
import com.qitmiaojie.pojo.User;
import com.qitmiaojie.service.MalfunctionService;
import com.qitmiaojie.mapper.MalfunctionMapper;
import com.qitmiaojie.service.SendEmailService;
import com.qitmiaojie.service.UserService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
@Slf4j
public class MalfunctionServiceImpl extends ServiceImpl<MalfunctionMapper, Malfunction>
    implements MalfunctionService{
    @Autowired
    private SendEmailService sendEmailService;
    @Autowired
    private UserService userService;
    @Autowired
    private MalfunctionMapper malfunctionMapper;

    @SneakyThrows
    @Override
    public Result insetMalfunction(Malfunction malfunction) {
        User CONTEXT_USER = userService.getByUsername(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        log.info(CONTEXT_USER.toString());
        malfunction.setUserid(CONTEXT_USER.getId());
        malfunction.setStatus(1);
        boolean flag = malfunctionMapper.insertMalfuncation(malfunction);
        if (flag) {
            sendEmailService.sendEmail(malfunction);
        }
        return Result.success("报修成功,同时宿管也会收到一条邮箱提示！",null);
    }

    @Override
    public Result getAllById() {
        User CONTEXT_USER = userService.getByUsername(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        List<Malfunction> malfunctionList = malfunctionMapper.selectAllById(CONTEXT_USER.getId());
        return Result.success(malfunctionList);
    }

    @Override
    public Result del(Integer id) {
        malfunctionMapper.removeMalfuncationById(id);
        return Result.success("取消报修成功！",null);
    }

    @Override
    public Result evaluate(Integer id, Integer value, String advice) {
        Malfunction malfunction = malfunctionMapper.selectMalfunctionById(id);
        if (malfunction.getStatus() != 3){
            log.info("没有完成报修不能评分！");
            return new Result(300,"维修没有完成不能评分！",null);
        }
        malfunctionMapper.evaluate(id,value.toString(),advice);
        return Result.success("评分成功！",null);
    }

    @Override
    public Result getMalfunctionAll(String date, String floor, String address, String phone, Integer status, Integer currentPage, Integer size) {
        Map<String,Object> map=new HashMap<>();
        if (!StringUtils.isEmpty(date)){
            int i = date.lastIndexOf("-");
            map.put("date",date.substring(0,i+3));
        }else {
            map.put("date",date);
        }


        map.put("floor",floor);
        map.put("address",address);
        map.put("phone",phone);
        map.put("status",status);
        map.put("start",(currentPage-1)*size);
        map.put("end",currentPage*size);
        Result result=new Result();
        Map<Object, Object> ResMap = MapUtil.builder()
                .put("data",null)
                .put("TotalCount", 0)
                .build();
        Integer TotalCount=malfunctionMapper.selectAllCount(map);
        int start = (currentPage-1)*size;
        int end = currentPage*size;
        List<Malfunction> malfunctionList =  malfunctionMapper.selectAll(map);
        ResMap.replace("data",malfunctionList);
        ResMap.replace("TotalCount",TotalCount);
        log.info(ResMap.toString());
        return Result.success(ResMap);
    }

    @Override
    public Result updateMalfunctionStatusById(Integer id, Integer status) {
        boolean flag = malfunctionMapper.updateStatusById(id,status);
        return flag ? Result.success("维修进度修改成功！"):Result.error("修改失败，服务器异常！");
    }


}




